// pages/hotel/hotelDetail/book/book.js
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponsList: [],
    price: 0,
    title: '',
    time: '',
    date: '', //日期
    dateStart: '', //开始日期
    dateEnd: '', //结束日期
    weekdayStart: '', //开始周几
    weekdayEnd: '', //结束周几
    show4: false, //日期组件是否展示
    show: false,
    hotelId: 0,
    roomId: 0,
    roomTypeId: 0,
    roomPrice: 0,
    roomList: [],
    roomCount: 0,
    value: 1,
    realName: '',
    phone: '',
    adult: '',
    child: '',
    fullPrice: 0,
    minusPrice: 0,
    people: '',
    array: ['14:00', '15:00', '16:00', '18:00'],
    objectArray: [{
        id: 0,
        name: '14:00'
      },
      {
        id: 1,
        name: '15:00'
      },
      {
        id: 2,
        name: '16:00'
      },
      {
        id: 3,
        name: '18:00'
      }
    ],
    index: 0,
    show: false,
    show1: false,
    show2: false,
    couponslist: [],
    userList:[],
    pageData:'',
    userId:''
  },
  //入店时间选择
  bindTimeChange(e) {
    this.setData({
      index: e.detail.value[0],
      time: this.data.array[e.detail.value[0]], // 设置入店时间
      selected: true,
    })
  },
  goBack() {
    wx.navigateTo({
      url: '/pages/hotel/hotelDetail/book/book',
    })
  },
  goSelect() {
    wx.navigateTo({
      url: '/pages/hotel/hotelDetail/book/occupantSelect/occupantSelect',
    })
  },
  onSubmit() {
    if(this.data.people==''|this.data.phone==''){
      wx.showToast({
        title: '入住人或手机号不能为空',
        icon:'none'
      })
    }else{
      wx.navigateTo({
        url: `/pages/hotel/hotelDetail/book/payOption/payOption?hotelId=${this.data.hotelId}&roomId=${this.data.roomId}&roomCount=${this.data.value}&startTime=${this.data.dateStart}&endTime=${this.data.dateEnd}&arrivalTime=${this.data.dateStart +' '+ this.data.array[this.data.index]}&money=${this.data.roomList[0].roomPrice.price*this.data.value-this.data.minusPrice}`,
        data:{
          hotelId: this.data.hotelId,
          userId: this.data.userId,
          roomId: this.data.roomId,
          roomCount: this.data.roomCount,
          startTime: this.data.dateStart,
          endTime: this.data.dateEnd,
          arrivalTime: this.data.dateStart + this.data.array[this.data.index],
          money: this.data.roomList[0].roomPrice.price*this.data.value-this.data.minusPrice
        }
      })
    }
  },
  useCoupon(e) {
    console.log("e",e)
    if(this.data.value*this.data.roomList[0].roomPrice.price<e.target.dataset.fullprice){
      // console.log("不满足条件")
      wx.showToast({
        title: '不满足条件',
        icon: 'none'
      })
    }else{
      this.setData({
        show: false,
        fullPrice: e.target.dataset.fullprice,
        minusPrice: e.target.dataset.minusprice,
      })
    }
  },
  roomChange:function(e){
    console.log("te",e.detail.value)
    this.setData({
      value: e.detail.value
    })
  },
  peopleChange:function(e){
    console.log("te",e.detail.value)
    this.setData({
      people: e.detail.value
    })
  },
  phoneChange:function(e){
    console.log("te",e.detail.value)
    this.setData({
      phone: e.detail.value
    })
  },
  showPopup1() {
    this.setData({
      show1: true
    });
  },
  onClose1() {
    this.setData({
      show1: false
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  openPopup() {
    this.setData({
      show: true
    })
  },
  /**
   * 日期改变触发事件
   */
  onDisplay() {
    this.setData({
      show4: true
    });
  },
  onClose4() {
    this.setData({
      show4: false
    });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getMonth() + 1}-${date.getDate()}`;
  },
  onConfirm(event) {
    const [start, end] = event.detail;
    var weeks = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
    var currentStart = start.getDay(); //指定开始日期
    var currentEnd = end.getDay(); //指定结束日期
    this.setData({
      show4: false,
      date: `${this.formatDate(start)} - ${this.formatDate(end)}`,
      dateStart: `${this.formatDate(start)}`,
      dateEnd: `${this.formatDate(end)}`,
      weekdayStart: weeks[currentStart],
      weekdayEnd: weeks[currentEnd],
    });
  },

  // 订 弹出层js 房型详情
  goBackHandle() {
    wx.navigateTo({
      url: '/pages/hotel/hotelDetail/book/book',
    })
  },
  showNotice() {
    Dialog.alert({
      title: '退订规则',
      message: '预定后30分钟内取消可全额推荐，06月21日后取消，将扣除100%房费',
      confirmButtonText: '我知道了',
    }).then(() => {
      // console.log('用户点击确定');
    });
  },
  //优惠券
  getCoupons(options) {
    wx.request({
      url: 'http://localhost:8080/hotel/coupons/list',
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      success: (resp) => {
        console.log("yh", resp.data.rows)
        this.setData({
          couponsList: resp.data.rows
        })
      }
    })
  },
  // 根据房型id和酒店id查询
  getRoomByTypeIdAndHotelId() {
    wx.request({
      url: 'http://localhost:8080/hotel/hotelRoom/list',
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {
        hotelId: this.data.hotelId,
        roomTypeId: this.data.roomTypeId
      },
      success: (resp) => {
        console.log("hr", resp.data.rows)
        this.setData({
          roomList: resp.data.rows
        })
        console.log("rl", this.data.roomList)
      }
    })
  },
  //获取入住人信息
  getUser(options){
    wx.request({
      url: 'http://localhost:8080/hotel/occupants/'+options.id,
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      success: (resp) => {
        console.log(resp)
        this.setData({
          userList: resp.data.data
        })
        console.log("getUser",resp.data)
      }
    })
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("op", options)
   this.getCoupons(options);
   this.getUser(options);
    this.setData({
      dateStart: options.dateStart, //开始日期
      dateEnd: options.dateEnd, //结束日期
      weekdayStart: options.weekdayStart, //开始周几
      weekdayEnd: options.weekdayEnd, //结束周几
      hotelId: options.hotelId,
      roomTypeId: options.roomTypeId,
      roomPrice: options.roomPrice,
      adult: parseInt(options.adult),
      child: parseInt(options.child),
      roomId: options.roomId
    })
    this.getCoupons(options);
    // this.setData({
    //   price: 320,
    //   title: '北京佳丽大酒店',
    //   value: '1',
    //   oneDay: 260,
    //   show: false
    // });
    this.showUserInfo();

    // 展示初始日期
    var date = new Date();
    var currentDate = date.getDay(); //获取存储当前日期
    var weekday = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];

    this.setData({
      dateStart: date.getMonth() + 1 + '-' + date.getDate(),
      dateEnd: date.getMonth() + 1 + '-' + (date.getDate() + 1),
      weekdayStart: weekday[currentDate],
      weekdayEnd: weekday[currentDate + 1],
    })

    this.getRoomByTypeIdAndHotelId()
  },
  // useCoupon() {
  //   this.setData({})
  // },
  showPopup() {
    this.setData({
      show: true
    });
  },

  // onClose() {
  //   this.setData({ show: false });
  // },
  showUserInfo() {
    const selectedGuest = wx.getStorageSync('selectedGuest'); // 从本地存储获取选定的客人信息
    if (selectedGuest) {
      // 显示用户信息
      // console.log('Selected Guest:', selectedGuest);
      // 在这里添加显示用户信息的逻辑
    } else {
      wx.showToast({
        title: '尚未选择客人',
        icon: 'none',
        duration: 2000
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow(e) {
    console.log(1212121,this.data.pageData);
    wx.request({
      url: 'http://localhost:8080/hotel/occupants/'+this.data.pageData.id,
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      success: (resp) => {
        console.log(resp)
        this.setData({
          userList: resp.data.data,
          userId:this.data.pageData.id
        })
        console.log("getUser",resp.data)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})